package logger

import (
    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
    "gopkg.in/natefinch/lumberjack.v2"
)

var Logger *zap.Logger

func InitLogger() {
    writeSyncer := getLogWriter()
    encoder := getEncoder()

    core := zapcore.NewCore(encoder, writeSyncer, zap.InfoLevel)

    Logger = zap.New(core, zap.AddCaller())
    zap.ReplaceGlobals(Logger)
}

func getLogWriter() zapcore.WriteSyncer {
    lumberJackLogger := &lumberjack.Logger{
        Filename:   "./logs/application.log",
        MaxSize:    10, // megabytes
        MaxBackups: 3,
        MaxAge:     28, // days
    }

    return zapcore.AddSync(lumberJackLogger)
}

func getEncoder() zapcore.Encoder {
    return zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
}